﻿using System;
using System.Web.UI;

using DotNetNuke.Entities.Users;
using DotNetNuke.Modules.UserDefinedTable.Security;


namespace DotNetNuke.Modules.UserDefinedTable
{


  public class EditCreatedBy : EditControl
  {
    protected string currentUserName;
    protected string viewStateName;

    //public EditCreatedBy()
    //{
    //    //base.Load += new EventHandler(this.Page_Load);
    //    //base.Init += new EventHandler(this.EditCreatedBy_Init);
    //}



    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
      //base.Init += new EventHandler(this.EditCreatedBy_Init);
      //base.Load += new EventHandler(this.Page_Load);

      this.currentUserName = UserController.GetCurrentUserInfo().Username;

      if ( string.IsNullOrEmpty(this.currentUserName) )
        this.currentUserName = Definition.NameOfAnonymousUser;

      this.viewStateName = "UDT-ECB" + this.FieldID.ToString() + this.FieldType;
      
    }





    //protected void Page_Load(object sender, EventArgs evt)
    protected override void OnLoad(EventArgs evt)
    {
      base.OnLoad(evt);

      if (this.Page.IsPostBack && !string.IsNullOrEmpty((string)this.ViewState[this.viewStateName]) )
        this.Controls.Add(new LiteralControl("<span class=\"Normal\">" + ModuleSecurity.BestUserName((string)this.ViewState[this.viewStateName], EditControl.PortalID) + "</span>"));

    }





    //private void EditCreatedBy_Init(object sender, EventArgs evt)
    //{
    //  this.currentUserName = UserController.GetCurrentUserInfo().Username;

    //  if (this.currentUserName == string.Empty)
    //    this.currentUserName = Definition.NameOfAnonymousUser;

    //  this.viewStateName = "UDT-ECB" + this.FieldID.ToString() + this.FieldType;
    //}




    public override string Value
    {
      get
      {
        if ( !string.IsNullOrEmpty((string)this.ViewState[this.viewStateName]) )
          return (string)this.ViewState[this.viewStateName];
        
        return this.currentUserName;
      }
      set
      {
        if ( !string.IsNullOrEmpty(value) )
        {
          this.ViewState[this.viewStateName] = value;
          this.Controls.Add(new LiteralControl("<span class=\"Normal\">" + ModuleSecurity.BestUserName(value, EditControl.PortalID) + "</span>"));
        }
      }
    }


  }

}

